<!--
@author  tanyuze
@desc    抽奖记录
@date    2022/11/26
-->
<template>
  <div class="draw-record-list">
    <el-collapse-transition>
      <section v-show="searchShow" class="main-search merge">
        <el-row>
          <el-col :span="8">
            <span class="svg-container">
              <svg-icon icon-class="search" />
            </span>
            <el-input v-model.trim="listQuery.userRealname" placeholder="输入用户名搜索" class="search-input" @keyup.enter.native="handleFilter" />
          </el-col>
          <el-col :span="8">
            <span class="svg-container">
              <svg-icon icon-class="search" />
            </span>
            <el-input v-model.trim="listQuery.areaName" placeholder="输入培训班名称搜索" class="search-input" @keyup.enter.native="handleFilter" />
          </el-col>
          <el-col :span="6" :offset="2">
            <section class="search-btn">
              <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
              <el-button type="primary" @click="handleReset">重置</el-button>
              <!--          <span class="search-more" @click="searchShow = !searchShow">
            更多筛选条件
            <i class="el-icon-arrow-down" />
          </span>-->
            </section>
          </el-col>
        </el-row>
        <!-- <span style="margin-left: 30px" class="svg-container">
          <svg-icon icon-class="search" />
        </span>
        <el-input
          v-model.trim="listQuery.recordTypeName"
          placeholder="输入关联活动"
          class="search-input"
          @keyup.enter.native="handleFilter"
        /> -->
      </section>
    </el-collapse-transition>
    <div class="page-btn">
      <div class="page-title">
        <span>培训记录</span>
      </div>

      <div class="page-btns">
        <el-button
          style="margin-left: 10px"
          @click="handleDownload"
        >导出
        </el-button>
      </div>
    </div>

    <div class="page-main-list">
      <v-table
        :list="list"
        tag="trainingAreaRecord"
        :source-arr="sourceArr"
        :selection="true"
        :list-loading="listLoading"
        :filter-column="filterColumn"
      >
        <!--        <el-table-column fixed="right" label="操作" width="160" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="handleDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>-->
      </v-table>
    </div>

    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getPage" />
  </div>
</template>
<script>
import Pagination from '@/components/Pagination'
import { RecordQueryPage, exportFile } from '../api'
import elDragDialog from '@/directive/el-drag-dialog'
import vTable from '@/components/Table'
import CryptoJS from '@/utils/aesJiaJM'
import { exportSign } from '@/utils/fileImportOrExportSign'
import { exportCheck } from '@/utils/sensitiveInfoExportCheck'

export default {
  name: 'TrainingAreaRecord',
  components: { Pagination, vTable },
  directives: { elDragDialog },
  data() {
    return {
      /* search*/
      searchShow: true,
      total: 0,
      listQuery: {
        page: 1,
        limit: 10,
        userRealname: '',
        areaName: ''
      },
      search: {
        title: ''
      },

      /* table*/
      tag: '',
      list: [],
      listLoading: false,
      sourceArr: ['courseImgUrl', 'imgIdUrl']

      /* user-defined*/
    }
  },
  computed: {
    /* tag() {
      return this.tagList[this.activeTab]
    }*/
  },
  watch: {
    /* activeTab() {
      this.myTime = new Date().getTime()
    }*/
  },
  created() {
    this.getPage()
  },
  methods: {
    // 导出文件 xlsx
    handleDownload() {
      // const link = document.createElement('a')
      // link.style.display = 'none'
      // link.href =
      //   '/API-ENTERPRISE-BACKEND/backend/areaHistoryRecord/export' +
      //   exportSign({ areaName: this.listQuery.areaName, userRealname: this.listQuery.userRealname }).paramStr
      // link.setAttribute('download', '培训记录.xlsx')
      // link.setAttribute('target', '_blank')
      // document.body.appendChild(link)
      // link.click()
      // document.body.removeChild(link)

      const loading = this.$loading({
        lock: true,
        text: '请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      })
      const query = exportSign({
        areaName: this.listQuery.areaName,
        userRealname: this.listQuery.userRealname
      }).obj
      exportFile(query)
        .then((res) => {
          loading.close()
          const content = res.data
          const blob = new Blob([content])
          const fileName = '培训记录.xlsx'
          if ('download' in document.createElement('a')) {
            const elink = document.createElement('a')
            elink.download = fileName
            elink.style.display = 'none'
            elink.href = URL.createObjectURL(blob)
            document.body.appendChild(elink)
            elink.click()
            URL.revokeObjectURL(elink.href) // 释放URL 对象
            document.body.removeChild(elink)
          } else {
            navigator.msSaveBlob(blob, fileName)
          }
        })
        .catch(() => {
          // this.$message.error()
          loading.close()
        })
    },
    exportFun() {
      var query = {}
      if (this.searchShow) {
        query['userRealname'] = this.listQuery.userRealname
        query['areaName'] = this.listQuery.areaName
      }
      exportCheck('WELFARE_DRAW_RECORD', '', recordExportMask, recordExportNoMask, query, '抽奖记录.xlsx')
    },
    filterColumn(scope, item) {
      if (item === 'userPhone') {
        return scope.row[item] ? CryptoJS.$aesJieM(scope.row[item]) : ''
      }
      return scope.row[item]
    },

    /**
     * 获取列表
     */
    getPage(obj) {
      this.listLoading = true
      var query = {
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit
      }
      if (this.searchShow) {
        query['userRealname'] = this.listQuery.userRealname
        query['areaName'] = this.listQuery.areaName
      }
      return new Promise((resolve, reject) => {
        RecordQueryPage(query)
          .then((response) => {
            this.listLoading = false
            this.list = response.data.data.rows
            this.total = response.data.data.total
          })
          .catch((error) => {
            this.listLoading = false
            reject(error)
          })
      })
    },

    /**
     *搜索
     */
    handleFilter() {
      this.listQuery.page = 1
      this.getPage()
    },

    /**
     *重置
     */
    handleReset() {
      this.listQuery.userRealname = ''
      this.listQuery.areaName = ''
      this.listQuery.page = 1
      this.getPage()
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss">
$dark_gray: #bfced6;
.draw-record-list {
  padding: 10px;

  .main-search {
    background-color: #fff;
    padding: 0 10px;
    .svg-container {
      padding: 0 5px 0 10px;
      color: #bfced6;
      vertical-align: middle;
      display: inline-block;
    }
    .search-input {
      width: 60%;
      position: relative;
      &:after {
        content: '';
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }
      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;
        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }
    .search-btn {
      display: inline-block;
      float: right;
      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }
      .search-more {
        font-size: 14px;
        font-weight: 500;
        @include font-color($mainColor);
        cursor: pointer;
        margin-left: 10px;
      }
    }

    &.merge {
      height: 50px;
      line-height: 50px;
    }

    &.separate {
      .search-inner {
        padding-top: 10px;
        .search-inner-item {
          .item-label {
            height: 30px;
            line-height: 30px;
            font-size: 14px;
            color: #a6a6a6;
            font-weight: 400;
          }
          .el-input {
            position: relative;
            &.el-date-editor input {
              padding-left: 30px;
            }
            .el-input__inner {
              border: none;
              padding: 0;
            }
            &:after {
              content: '';
              height: 1px;
              background-color: #e2e3e8;
              position: absolute;
              left: 0;
              right: 0;
              bottom: 0;
            }
          }
        }
      }
      .search-btn {
        text-align: center;
        display: block;
        float: none;
        height: 56px;
        line-height: 56px;
      }
    }
  }

  .page-btn {
    display: flex;
    height: 46px;
    line-height: 46px;
    width: 100%;

    .page-title {
      flex: 1;
      font-size: 16px;
      font-weight: bold;
      color: rgba(51, 51, 51, 1);
    }
  }

  .page-tabs {
    width: 100%;
    margin: 10px 0;
    padding: 0 10px;
    background-color: #fff;
    border-radius: 4px;

    .el-tabs__nav-next {
      line-height: 40px;
    }

    .el-tabs__nav-wrap::after {
      display: none;
    }

    .el-tabs__header {
      margin: 0;
    }
  }

  .page-main-list {
    .el-image {
      width: 100%;
      height: auto;
      max-height: 140px;
    }
  }

  .pagination-container {
    background: none;
  }
}
</style>
